<?php

namespace plugin\cms\app\controller\admin\cnf;

use core\controller\AdminBase;
use Exception;
use plugin\cms\app\bll\Rewrite as BllRewrite;
use support\View;

/**
 * rewrite ^/app_cms_page/(\d+)/(\d+).html  /app/cms/home/Index/page?cid=$1&id=$2 last;

 */
class Rewrite extends AdminBase
{
    /** */
    public function index()
    {
        $bllRewrite = new BllRewrite();
        $rewrites   = $bllRewrite->select();
        View::assign('rewrites', $rewrites);
        return $this->view();
    }

    public function useRule()
    {
        try {
            $bllRewrite = new BllRewrite();
            $bllRewrite->setUse(
                request()->post('id'),
                request()->post('use')
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    public function addRule()
    {
        try {
            $bllRewrite = new BllRewrite();
            $bllRewrite->add(request()->post());
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    public function delRule()
    {
        try {
            $bllRewrite = new BllRewrite();
            $bllRewrite->delete(request()->post('id'));
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
